<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>tinymce.util.Dispatcher tests</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-git.css" type="text/css" />
<script src="http://code.jquery.com/qunit/qunit-git.js"></script>
<script src="qunit/connector.js"></script>
<script type="text/javascript" src="qunit/runner.js"></script>
<script type="text/javascript" src="js/utils.js"></script>
<script type="text/javascript" src="js/tiny_mce_loader.js"></script>
<script>
module("tinymce.util.Dispatcher");

QUnit.config.reorder = false;

(function() {
	var Dispatcher = tinymce.util.Dispatcher;

	test('dispatch with arguments', function() {
		var ev, v, f;

		ev = new Dispatcher();
		ev.add(function(a, b, c) {
			v = a + b + c;
		});
		ev.dispatch(1, 2, 3);
		equal(v, 6);
	});

	test('remove callback', function() {
		var ev, v, f;

		ev = new Dispatcher();
		v = 0;
		f = ev.add(function(a, b, c) {
			v = a + b + c;
		});
		ev.remove(f);
		ev.dispatch(1, 2, 3);
		equal(v, 0);
	});

	test('default scope', function() {
		var ev, v, f;

		ev = new Dispatcher({test : 1});
		v = 0;
		f = ev.add(function(a, b, c) {
			v = a + b + c + this.test;
		});
		ev.dispatch(1, 2, 3);
		equal(v, 7);
	});

	test('callback scope', function() {
		var ev, v, f;

		ev = new Dispatcher();
		v = 0;
		f = ev.add(function(a, b, c) {
			v = a + b + c + this.test;
		}, {test : 1});
		ev.dispatch(1, 2, 3);
		equal(v, 7);
	});

	test('addToTop', function() {
		var ev, v, f;

		ev = new Dispatcher();
		v = '';
		f = ev.add(function(a, b, c) {
			v += 'b';
		}, {test : 1});
		f = ev.addToTop(function(a, b, c) {
			v += 'a';
		}, {test : 1});
		ev.dispatch();
		equal(v, 'ab');
	});

	test('addToTop in loop', function() {
		var ev, v, f;

		ev = new Dispatcher();
		v = '';
		f = ev.add(function(a, b, c) {
			f = ev.addToTop(function(a, b, c) {
				v += 'a';
			}, {test : 1});

			v += 'b';
		}, {test : 1});

		ev.dispatch();
		equal(v, 'b');
	});
})();
</script>
</head>
<body>
	<h1 id="qunit-header">tinymce.util.Dispatcher tests</h1>
	<h2 id="qunit-banner"></h2>
	<div id="qunit-testrunner-toolbar"></div>
	<h2 id="qunit-userAgent"></h2>
	<ol id="qunit-tests"></ol>
	<div id="content"></div>
</body>
</html>
